package com.cljz.peipei.ui.activity;

import android.app.Activity;
import android.os.Bundle;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;

import com.cljz.peipei.R;
import com.cljz.peipei.base.App;
import com.cljz.peipei.base.BaseActivity;
import com.cljz.peipei.model.net.bean.ResponseBean;
import com.cljz.peipei.present.activity.BindP;
import com.cljz.peipei.present.activity.LoginP;
import com.cljz.peipei.utils.CommonUtils;
import com.cljz.peipei.utils.T;
import com.jakewharton.rxbinding.view.RxView;
import com.jakewharton.rxbinding.widget.RxTextView;

import butterknife.BindView;
import butterknife.ButterKnife;
import cn.droidlover.xdroidmvp.router.Router;
import rx.functions.Action1;

/**
 * Created by Administrator on 2017/5/2 0002.
 */
public class BindphoneActivity extends BaseActivity<BindP> {
    @BindView(R.id.et_number)
    EditText etNumber;
    @BindView(R.id.ll_number)
    LinearLayout llNumber;
    @BindView(R.id.btn_get_sms)
    Button btnGetSms;
    private String phone;

    public void sendSmsSucceed(ResponseBean peopleNumBean) {
        dismissLoadingDialog();
        Router.newIntent((Activity) App.getInstance()).putString("phone", phone).to(EditSmsCodeActivity.class).launch();
//        Router.pop(this);
    }

    public void sendSmsFail(String error) {
        dismissLoadingDialog();
        T.getInstance().showShort(error);
    }

    @Override
    public void initData(Bundle savedInstanceState) {

    }

    @Override
    public int getLayoutId() {
        return R.layout.activity_bindphone;
    }

    @Override
    public BindP newP() {
        return new BindP();
    }

    @Override
    protected void initListener() {
        super.initListener();
        RxTextView.textChanges(etNumber)
                .compose(this.<CharSequence>bindToLifecycle())
                .subscribe(new Action1<CharSequence>() {
                    @Override
                    public void call(CharSequence charSequence) {
                        if (charSequence.length() == 11) {
                            if (!CommonUtils.checkPhone(charSequence.toString())) {
                                T.getInstance().showShort("请输入正确的手机号");
                            }
                        }
                    }
                });
        RxView.clicks(btnGetSms)
                .compose(this.<Void>bindToLifecycle())
                .subscribe(new Action1<Void>() {
                    @Override
                    public void call(Void aVoid) {
                        showLoadingDialog();
//                Router.newIntent((Activity) App.getInstance()).putString("phone", phone).to(EditSmsCodeActivity.class).launch();

                        if (CommonUtils.isNetworkAvailable()) {
                            phone = etNumber.getText().toString().trim();
                            if (CommonUtils.checkPhone(phone)) {
                                getP().sendSmsCode(phone);
                            } else {
                                T.getInstance().showShort("请输入正确的手机号");
                            }
                        } else {
                            T.getInstance().showShort("请开启网络");
                        }
                    }
                });
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        // TODO: add setContentView(...) invocation
        ButterKnife.bind(this);
    }
}
